<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class control_attach extends base_control{

	//hook ctr_attach_start.php
	public function __construct() {
	
		//hook ctr_attach_init_before.php
		parent::__construct();
		
		//hook ctr_attach_init_end.php
	}
	
	public function do_load() {
		//hook ctr_attach_load_start.php
		$this->check_user_login_msg();
		
		//hook ctr_attach_load_before_md_get.php
		$md5 = str_replace('-','',core::gpc('md5','G'));
		if(empty($md5)) $this->message('下载失败，不正确的md5');

		//hook ctr_attach_load_before_tmp_get.php
		$tmp = $this->db->get("attach-md5-$md5");
		if(empty($tmp)) $this->message('下载失败，未找到此文件');
		
		if(!misc::in_arr_str($tmp['path'], $this->confs['_file_imgs_ext'])) {
			$core_minus = 2;//扣2分
			$core_add = 1;//加1分
			
			//hook ctr_attach_load_before_checke_core.php
			$iii = $this->user->user_core();
			if($iii['core'] < $core_minus)
				$this->message('下载失败，剩余积分不足，需扣除'.$core_minus.'，积分剩余:'.$iii['core']);
			
			//hook ctr_attach_load_before_minus_core.php
			if(!$this->user->user_core_edit('-', $core_minus))
				$this->message('下载失败，扣除积分失败');
			
			//hook ctr_attach_load_before_get_author.php
			$author = $this->user->get_user_by_uid($tmp['uid'], false);
			if(empty($author)) $this->message('下载失败，未找到上传者');
			
			//hook ctr_attach_load_before_add_core.php
			if(!$this->user->user_core_edit('+', $core_add, $author['uid'], $author['core']))
				$this->message('下载失败，增加上传者积分失败');
		}
		//hook ctr_attach_load_before_end.php
		$infos = core::json_decode($tmp['infos']);
		$token = md5(date('YmdH').CONFIG_CS_KEY);
		$fname = $infos == false ? '' : encrypt::en($infos['fname'], $token);
		html::html_locate('jy_server/server_download.php?f='.base64_encode(encrypt::en($tmp['path'], $token).'|'.$fname));
	}
	//hook ctr_attach_end.php

}
/*{TranslateTagEnd}*/

?>